﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MyBet.Web;
using MyBet.Class;
using ParieurPro.DAL;

namespace MyBet
{
	public partial class MatchControl : UserControl
	{
        private DaysList.DayStatus _dayStatus;
        private int _matchId;
        private EventServiceReference.MatchObject _currentMatch;

        public enum PredictionType
        {
            HomeWin, DrawGame, AwayWin
        }

        public int MatchId
        {
            get { return _matchId; }
            set { _matchId = value; }
        }

        public DaysList.DayStatus DayStatus
        {
            get { return _dayStatus; }
            set { _dayStatus = value; }
        }

        public EventServiceReference.MatchObject CurrentMatch
        {
            get { return _currentMatch; }
            set { _currentMatch = value; }
        }

		public MatchControl()
		{
			// Required to initialize variables
			InitializeComponent();
            DayStatus = DaysList.DayStatus.NotStarted;
		}

        public void LoadMatch(TeamObject tHome, TeamObject tAway)
        {
            TeamHomeImage.Source = ImageUtil.byteArrayToImage(tHome.Image);
            TeamHomeName.Text = tHome.Name;
            TeamAwayImage.Source = ImageUtil.byteArrayToImage(tAway.Image);
            TeamAwayName.Text = tAway.Name;
        }


        private PredictionType GetPredictionType(int scoreHome, int scoreAway)
        {
            if (scoreHome > scoreAway)
            {
                return PredictionType.HomeWin;
            }
            else if (scoreAway > scoreHome)
            {
                return PredictionType.AwayWin;
            }
            else
            {
                return PredictionType.DrawGame;
            }
        }

        private bool IsGoodPrediction(int scoreHome, int scoreAway)
        {
            if ((CurrentMatch._teamHomeScore.HasValue) && (CurrentMatch._teamAwayScore.HasValue))
            {
                if (GetPredictionType(scoreHome, scoreAway) == GetPredictionType(CurrentMatch._teamHomeScore.Value, CurrentMatch._teamAwayScore.Value))
                {
                    return true;
                }
            }
            return false;
        }

        public void SetBet(int scoreHome, int scoreAway)
        {
            if ((DayStatus == DaysList.DayStatus.Bet)|| (DayStatus == DaysList.DayStatus.NotStarted))
            {
                TeamHomeScore.SelectedIndex = scoreHome;
                TeamAwayScore.SelectedIndex = scoreAway;
            }
            else if (DayStatus == DaysList.DayStatus.Finished)
            {
                BetText.Text = String.Format("({0} - {1})", scoreHome.ToString(), scoreAway.ToString());
                if ((CurrentMatch._teamHomeScore.HasValue) && (CurrentMatch._teamAwayScore.HasValue))
                {
                    if (((scoreHome == CurrentMatch._teamHomeScore)) && (scoreAway == CurrentMatch._teamAwayScore))
                    {
                        GoodScoreIcon.Visibility = Visibility.Visible;
                    }
                    else if (IsGoodPrediction(scoreHome, scoreAway))
                    {
                        GoodPredictionIcon.Visibility = Visibility.Visible;
                    }
                }
            }
        }

        public void LoadFrom(EventServiceReference.MatchObject m)
        {
            CurrentMatch = m;
            LoadMatch(App.TeamsCache[m._teamHomeId], App.TeamsCache[m._teamAwayId]);
            if (m._displayName == "1")
            {
                TeamHomeName.Foreground = new SolidColorBrush(Colors.Red);
                TeamAwayName.Foreground = new SolidColorBrush(Colors.Red);
            }
            else
            {
                TeamHomeName.Foreground = new SolidColorBrush(Colors.Black);
                TeamAwayName.Foreground = new SolidColorBrush(Colors.Black);          
            }
        
            if ((DayStatus == DaysList.DayStatus.Bet) || (DayStatus == DaysList.DayStatus.NotStarted))
            {
                TeamHomeScoreTxt.Visibility = Visibility.Collapsed;
                TeamAwayScoreTxt.Visibility = Visibility.Collapsed;
                TeamHomeScore.Visibility = Visibility.Visible;
                TeamAwayScore.Visibility = Visibility.Visible;
                BetText.Visibility = Visibility.Collapsed;
                TeamHomeScore.SelectedValue = -1;
                TeamAwayScore.SelectedValue = -1;
                //if (m.TeamHomeScore.HasValue)
                //{
                //    TeamHomeScore.SelectedValue = m.TeamHomeScore.Value.ToString();
                //}
                //if (m.TeamAwayScore.HasValue)
                //{
                //    TeamAwayScore.SelectedValue = m.TeamAwayScore.Value.ToString();
                //}
            }
            else if (DayStatus == DaysList.DayStatus.Finished)
            {
                TeamHomeScoreTxt.Visibility = Visibility.Visible;
                TeamAwayScoreTxt.Visibility = Visibility.Visible;
                TeamHomeScore.Visibility = Visibility.Collapsed;
                TeamAwayScore.Visibility =  Visibility.Collapsed;
                BetText.Visibility = Visibility.Visible;
                if (m._teamHomeScore.HasValue)
                {
                    TeamHomeScoreTxt.Text = m._teamHomeScore.Value.ToString();
                }
                else
                {
                    TeamHomeScoreTxt.Text = "?";
                }
                if (m._teamAwayScore.HasValue)
                {
                    TeamAwayScoreTxt.Text = m._teamAwayScore.Value.ToString();
                }
                else
                {
                    TeamAwayScoreTxt.Text = "?";
                }
            }
            //else if (DayStatus == DaysList.DayStatus.NotStarted)
            //{
            //    TeamHomeScoreTxt.Visibility = Visibility.Visible;
            //    TeamAwayScoreTxt.Visibility = Visibility.Visible;
            //    TeamHomeScore.Visibility = Visibility.Collapsed;
            //    TeamAwayScore.Visibility = Visibility.Collapsed;
            //    if (m.TeamHomeScore.HasValue)
            //    {
            //        TeamHomeScoreTxt.Text = m.TeamHomeScore.Value.ToString();
            //    }
            //    if (m.TeamAwayScore.HasValue)
            //    {
            //        TeamAwayScoreTxt.Text = m.TeamAwayScore.Value.ToString();
            //    }
            //}
        }

	}
}